package animation.wlh.com.animation_demo

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.ListView
import java.util.jar.Attributes

/**
 * Created by weilh on 2016/1/26.
 */
class ScrollListView : ListView {
    constructor(context : Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
    constructor(context: Context, attributeSet: AttributeSet, style : Int) : super(context, attributeSet, style)
    var mListener : ListScrollListener? = null
    public interface  ListScrollListener {
        fun scroll(y : Float = 0f)
        fun scrollEnd()
    }

    var mDownY : Float = 0f;

    fun setListener(listener : ListScrollListener) {
        mListener = listener;
    }

    override fun onTouchEvent(ev: MotionEvent?): Boolean {
        if (ev == null) return false
        if (mListener == null) return super.onTouchEvent(ev)
        when(ev.action) {
            MotionEvent.ACTION_DOWN -> {
                mDownY = ev.rawY
            }
            MotionEvent.ACTION_UP -> {
                mListener?.scrollEnd()
            }
            MotionEvent.ACTION_MOVE -> {
                ev.rawY - mDownY
                if (Math.abs(ev.rawY - mDownY) < 3) return super.onTouchEvent(ev)
                mListener?.scroll(ev.rawY - mDownY)
                mDownY = ev.rawY
            }
        }
        return super.onTouchEvent(ev)
    }
}